package mumu.android.view;

import mumu.android.R;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Build;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Scroller;

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public class scroll_view extends Activity {
	private static final String TAG = "dd";
	LinearLayout lay1, lay2, lay0;
	private Scroller mScroller;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mScroller = new Scroller(this);
		lay1 = new MyLinearLayout(this);
		lay2 = new MyLinearLayout(this);

		lay1.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_launcher));
		lay2.setBackgroundColor(this.getResources().getColor(android.R.color.darker_gray));
		lay0 = new ContentLinearLayout(this);
		lay0.setOrientation(LinearLayout.VERTICAL);
		LinearLayout.LayoutParams p0 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
		this.setContentView(lay0, p0);

		LinearLayout.LayoutParams p1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
		p1.weight = 1;
		lay0.addView(lay1, p1);
		LinearLayout.LayoutParams p2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
		p2.weight = 1;
		lay0.addView(lay2, p2);
		MyButton btn1 = new MyButton(this);

		MyButton btn2 = new MyButton(this);
		btn1.setText("btn in layout1");
		btn2.setText("btn in layout2");
		final CustomView cvCustomView=new CustomView(this);
		cvCustomView.addView(btn1);
		cvCustomView.setBackgroundColor(getResources().getColor(android.R.color.black));
		btn1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mScroller.startScroll(0, 0, -100, -100, 500);
				// 开始滚动，设置初始位置--》结束位置 & 持续时间
			}
		});
		btn2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//mScroller.startScroll(0, 0, -300, -300, 1000);
				cvCustomView.smoothScrollTo(-300, -300);
			}
		});
		
		lay1.addView(cvCustomView,p1);
		//lay1.addView(btn1);
		lay2.addView(btn2);
	}

	class MyButton extends Button {
		public MyButton(Context ctx) {
			super(ctx);
		}

		@Override
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			Log.d(TAG, this.toString() + " onDraw------");
		}
	}

	class MyLinearLayout extends LinearLayout {
		public MyLinearLayout(Context ctx) {
			super(ctx);
		}

		@Override
		public void computeScroll() {
			Log.d(TAG, this.toString() + " computeScroll-----------");
			if (mScroller.computeScrollOffset())// 如果mScroller没有调用startScroll，这里将会返回false。
			{
				// 因为调用computeScroll函数的是MyLinearLayout实例，
				// 所以调用scrollTo移动的将是该实例的孩子，也就是MyButton实例
				scrollTo(mScroller.getCurrX(), 0);
				Log.d(TAG, "getCurrX = " + mScroller.getCurrX());

				// 继续让系统重绘
				invalidate();
			}
		}
	}
	
	

	class ContentLinearLayout extends LinearLayout {
		public ContentLinearLayout(Context ctx) {
			super(ctx);
		}

		@Override
		protected void dispatchDraw(Canvas canvas) {
			Log.d(TAG, "contentview dispatchDraw");
			super.dispatchDraw(canvas);
		}

		@Override
		public void computeScroll() {
			// 先判断mScroller滚动是否完成
			if (mScroller.computeScrollOffset()) {

				// 这里调用View的scrollTo()完成实际的滚动
				scrollTo(mScroller.getCurrX(), mScroller.getCurrY());

				// 必须调用该方法，否则不一定能看到滚动效果
				postInvalidate();
			}
			super.computeScroll();
		}
	}

	public class CustomView extends LinearLayout {

		private static final String TAG = "Scroller";

		private Scroller mScroller;

		public CustomView(Context context) {
			super(context);
			mScroller = new Scroller(context);
		}

		// 调用此方法滚动到目标位置
		public void smoothScrollTo(int fx, int fy) {
			int dx = fx - mScroller.getFinalX();
			int dy = fy - mScroller.getFinalY();
			smoothScrollBy(dx, dy);
		}

		// 调用此方法设置滚动的相对偏移
		public void smoothScrollBy(int dx, int dy) {

			// 设置mScroller的滚动偏移量
			mScroller.startScroll(mScroller.getFinalX(), mScroller.getFinalY(), dx, dy,1000);
			invalidate();// 这里必须调用invalidate()才能保证computeScroll()会被调用，否则不一定会刷新界面，看不到滚动效果
		}

		@Override
		public void computeScroll() {

			// 先判断mScroller滚动是否完成
			if (mScroller.computeScrollOffset()) {

				// 这里调用View的scrollTo()完成实际的滚动
				scrollTo(mScroller.getCurrX(), mScroller.getCurrY());

				// 必须调用该方法，否则不一定能看到滚动效果
				postInvalidate();
			}
			super.computeScroll();
		}
	}
}